package com.cyh.myapplication.weex;

/**
 * @author youhai.cai
 *         create by 2018/5/9 15:09.
 */
import android.content.Context;
import android.graphics.BitmapFactory;
import android.text.TextUtils;
import android.util.Log;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.cyh.myapplication.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.taobao.weex.adapter.IWXImgLoaderAdapter;
import com.taobao.weex.common.WXImageStrategy;
import com.taobao.weex.dom.WXImageQuality;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;

/**
 * Created by lixinke on 16/6/1.
 */
public class ImageAdapter implements IWXImgLoaderAdapter {
    private Context mContex;
      public ImageAdapter(Context context){
          mContex=context;
      }
      /*
        js中图片的格式可能有三种
        1.请求网络图片。
        2.请求的是资源文件drawable   drawable://test
        3.请求的是资源文件mipmap     mipmap://test
        4.请求的是资源文件assets    assets://test.jpg
        5.请求的是具体文件路径    file://sdcard/Download/24574411554_5555555.jpg
       */
    @Override
    public void setImage(String url, ImageView view, WXImageQuality quality, WXImageStrategy strategy) {
        //实现你自己的图片下载，否则图片无法显示。
        view.setScaleType(ImageView.ScaleType.FIT_XY);
        //url="mipmap://ic_launcher";
        url="assets://ic_launcher.png";
        Object object=url;
        if(!TextUtils.isEmpty(url)){//判断是否是 drwable或者mipmap
            if(url.startsWith("drawable://")||url.startsWith("mipmap://")){
                object=getImageIdByReflect(url);
              // view.setImageResource(drawableId);
            }else if(url.startsWith("assets")){
                try {
                    object= BitmapFactory.decodeStream(mContex.getAssets().open(url.split("assets://")[1]));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
         Glide.with(mContex)
                  .load(object)
                  .into(view);
                  
                  
/*
                  
WXSDKManager.getInstance().postOnUiThread(new Runnable() {

        @Override
        public void run() {
            if(view==null||view.getLayoutParams()==null){
        return;
        }
        if (TextUtils.isEmpty(url)) {
            view.setImageBitmap(null);
            return;
        }
        String temp = url;
        if (url.startsWith("//")) {
            temp = "http:" + url;
        }
        if (view.getLayoutParams().width <= 0 || view.getLayoutParams().height <= 0) {
            return;
        }
        Picasso.with(WXEnvironment.getApplication())
        .load(temp)
        .into(view);
        }
    },0);

 */
    }

    /**
     * 通过反射的方式获取资源文件中对应文件名的id
     * @param url
     * @return
     */
    private int getImageIdByReflect(String url) {
        Class drawable=null;
        if(url.startsWith("drawable://")){
            drawable = R.drawable.class;
            url=url.split("drawable://")[1];
        }else if(url.startsWith("mipmap://")){
            drawable = R.mipmap.class;
            url=url.split("mipmap://")[1];
        }

        Field fields[] =drawable.getDeclaredFields();
        try {
        for(Field field:fields){
            if(url.equals(field.getName())){
                int resId=field.getInt(field);
                return resId;
            }
        }
        }  catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return 0;
    }

}